<template>
  <div class="kxy-container">
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()" @refreshDataList="getDataList">新增</el-button>
        <el-button
          v-if="isAuth('sys:user:delete')"
          type="danger"
          @click="delOrUpdateHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      style="flex: 1"
      height="600"
      @selection-change="selectionChangeHandle"
      v-loading="dataListLoading"
    >
<!--      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
      <el-table-column align="center" type="index" header-align="center" width="80px" label="序号"></el-table-column>
      <!--        <el-table-column-->
      <!--          prop="positionBrief"-->
      <!--          header-align="center"-->
      <!--          align="center"-->
      <!--          label="广告位置">-->
      <!--          <template slot-scope="scope">-->
      <!--            <icon-svg :name="scope.row.positionBrief || ''"></icon-svg>-->
      <!--          </template>-->
      <!--        </el-table-column>-->
      <el-table-column align="center" prop="positionBrief" header-align="center" label="广告位置描述"></el-table-column>
      <el-table-column align="center" prop="code" width="150" header-align="center" label="广告位置编码"></el-table-column>
      <el-table-column label="排序" align="center" width="80">
        <template slot-scope="scope">
          <span>{{scope.row.sort}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="state" header-align="center" align="center" width="80" label="是否禁用">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state==1">是</el-tag>
          <el-tag v-else>否</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="createTime" header-align="center" label="创建时间"></el-table-column>
      <el-table-column align="center" prop="updateTime" header-align="center" label="更新时间"></el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="editOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="text" @click="delOrUpdateHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!--      新增-->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <edit-or-update v-if="editOrUpdateVisible" ref="editOrUpdate" @refreshDataList="getDataList"></edit-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './adposition-add'
import EditOrUpdate from './adposition-edit'

export default {
  name: 'adposition',
  data() {
    return {
      dataList: [],
      dataListSelections: [],
      pageIndex: 1,
      pageSize: 10,
      addOrUpdateVisible: false,
      editOrUpdateVisible: false,
      totalPage: 0,
      dataListLoading: false
    }
  },
  components: {
    AddOrUpdate,
    EditOrUpdate
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: '/adposition/list',
        method: 'post',
        data: {
          page: this.pageIndex,
          limit: this.pageSize
        }
      }).then(({ data }) => {
        if (data) {
          this.dataList = data.list
          this.totalPage = data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    editOrUpdateHandle(id) {
      this.editOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.editOrUpdate.init(id)
      })
    },
    delOrUpdateHandle(id) {
      this.$confirm(`确定要进行删除操作吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(_ => {
        this.$http({
          url: '/adposition/delete',
          method: 'post',
          data: {
            ids: id || this.dataListSelections.map(item => item.id).join(',')
          }
        }).then(data => {
          if (data && data.code === 200) {
            this.$notify.success({
              title: '提示',
              message: '操作成功'
            })
            this.getDataList()
          } else {
            this.$notify.warning({
              title: '提示',
              message: data.msg
            })
          }
        })
      })
    }
  }
}
</script>

<style scoped>
</style>
